package com.zmj.sy.mom.srv.aps.utils.diff;

import cn.hutool.core.collection.CollUtil;
import com.zmj.sy.mom.srv.aps.bean.entity.base.BaseEntity;
import com.zmj.sy.mom.srv.aps.bean.entity.order.ApsWorkDetail;
import com.zmj.sy.mom.srv.aps.bean.entity.order.ApsWorkOrder;
import com.zmj.sy.mom.srv.aps.bean.entity.order.Order;
import com.zmj.sy.mom.srv.aps.bean.vo.consistency.ConsistencyCheckDataResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.consistency.ConsistencyCheckReqVo;
import com.zmj.sy.mom.srv.aps.mapper.ApsWorkDetailMapper;
import com.zmj.sy.mom.srv.aps.mapper.ApsWorkOrderMapper;
import com.zmj.sy.mom.srv.aps.mapper.OrderMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Component
public class ApsDiffRule implements DiffRule {

    private final OrderMapper orderMapper;
    private final ApsWorkDetailMapper apsWorkDetailMapper;
    private final ApsWorkOrderMapper apsWorkOrderMapper;

    @Override
    public List<ConsistencyCheckDataResVo> getData(ConsistencyCheckReqVo reqVo) {

        List<Order> orderList = orderMapper.lambdaQuery().eq(Order::getConstructNo, reqVo.getConstructionCode()).list();
        if(CollUtil.isEmpty(orderList)){
            return new ArrayList<>();
        }

        List<Integer> orderIdList = orderList.stream().map(BaseEntity::getId).collect(Collectors.toList());

        List<ApsWorkDetail> workDetailList = apsWorkDetailMapper.lambdaQuery()
                .in(ApsWorkDetail::getOrderId, orderIdList)
                .in(ApsWorkDetail::getPhaseCode, "JG", "HG")
                .eq(ApsWorkDetail::getType, 2)
                .list();
        List<Integer> workOrderIdList = workDetailList.stream().map(ApsWorkDetail::getApsWorkOrderId).distinct().collect(Collectors.toList());

        List<ApsWorkOrder> workOrderList = apsWorkOrderMapper.lambdaQuery()
                .in(ApsWorkOrder::getId, workOrderIdList)
                .list();
        Map<Integer, ApsWorkOrder> workOrderMap = workOrderList.stream().collect(Collectors.toMap(BaseEntity::getId, e -> e));

        List<Integer> collect = workDetailList.stream().map(ApsWorkDetail::getOrderId).distinct().collect(Collectors.toList());
        List<Order> orderListNew = orderMapper.lambdaQuery().in(Order::getId, collect).list();
        Map<Integer, Order> orderMap = orderListNew.stream().collect(Collectors.toMap(BaseEntity::getId, Function.identity()));

        List<ConsistencyCheckDataResVo> list = new ArrayList<>();
        for (ApsWorkDetail apsWorkDetail : workDetailList) {
            ConsistencyCheckDataResVo resVo = new ConsistencyCheckDataResVo();
            list.add(resVo);

            resVo.setPlmId(apsWorkDetail.getPlmId());
            resVo.setMaterName(apsWorkDetail.getMaterName());
            resVo.setTotalCount(apsWorkDetail.getTotalCount().intValue());


            if(apsWorkDetail.getApsWorkOrderId() == null){
                continue;
            }
            ApsWorkOrder apsWorkOrder = workOrderMap.get(apsWorkDetail.getApsWorkOrderId());
            String[] split = apsWorkOrder.getStandards().split("δ");

//            if(split.length == 2){
//                resVo.setMaterial(split[0].trim());
//                resVo.setThickness(Integer.parseInt(split[1].trim()));
//            } else {
//                resVo.setMaterial(apsWorkOrder.getTexture());
//                resVo.setThickness(apsWorkOrder.getMaterHigh());
//            }

            if (!StringUtils.hasText(apsWorkOrder.getTexture())) {
                resVo.setMaterial(split[0].trim());
            } else {
                resVo.setMaterial(apsWorkOrder.getTexture());
            }

            if (apsWorkOrder.getMaterHigh() == null) {
                resVo.setThickness(Integer.parseInt(split[1].trim()));
            } else {
                resVo.setThickness(apsWorkOrder.getMaterHigh());
            }
//            resVo.setMaterial(split[0].trim());
//            resVo.setThickness(Integer.parseInt(split[1]));
            resVo.setProcessChain(apsWorkOrder.getWorkChain());
            resVo.setDrawCode(apsWorkOrder.getDrawing());
            resVo.setWorkOrderCode(apsWorkOrder.getWorkOrderCode());
            resVo.setErpCode(apsWorkOrder.getErpCode());

            Order order = orderMap.get(apsWorkOrder.getOrderId());
            resVo.setConstructionCode(order.getConstructNo());

        }

        Map<String, ConsistencyCheckDataResVo> collect1 = list.stream()
                .collect(Collectors.toMap(ConsistencyCheckDataResVo::getWorkOrderCode, Function.identity(), (a, b) -> {
                    a.setTotalCount(a.getTotalCount() + b.getTotalCount());
                    return a;
                }));

        return new ArrayList<>(collect1.values());
    }

    @Override
    public String getCode() {
        return APS_CODE;
    }
}
